package cn.nju.edu.software.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.edu.nju.software.model.Contact;
import cn.edu.nju.software.model.User;
import cn.edu.nju.software.service.IContactService;
import cn.edu.nju.software.service.ILoginService;
import cn.edu.nju.software.serviceConfig.ClientServiceHelper;
import cn.nju.edu.software.mgr.UserMgr;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class TestNetActivity extends Activity {

	private String key = "\n";
	private Button btn1;
	private Button btn2;
	private Button btn3;
	private Button btn4;
	private Button btn5;
	private TextView tv;
	private User user;
	private Contact nowContact;
	private List<Contact> contacts;
	private UserMgr userMgr;
	private Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.testnet_activity);
		context = this;
		userMgr = new UserMgr(context);
		btn1 = (Button) findViewById(R.id.button1);
		btn2 = (Button) findViewById(R.id.button2);
		btn3 = (Button) findViewById(R.id.button3);
		btn4 = (Button) findViewById(R.id.button4);
		btn5 = (Button) findViewById(R.id.button5);
		tv = (TextView) findViewById(R.id.tv);

		user = new User();
		user.setUserName("ysm");
		user.setPasswd("123456");
		user.setId(1);
		user.setContactId(1000);
		user.setType(1);
		btn1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				ILoginService ls = ClientServiceHelper.getLoginService();
				Map<String, Object> result = ls.login(user.getUserName(),user.getPasswd());
				String status = (String) result.get("status");
				String msg = (String) result.get("msg");
				User u = (User) result.get("User");
				String str = status + msg + u.getId() + u.getUserName()
						+ u.getPasswd() + u.getContactId() + u.getType();
				tv.setText(str);
			}
		});
		btn2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				IContactService cs = ClientServiceHelper.getContactService();
				contacts = new ArrayList<Contact>();
				contacts = cs.getContactList();
				String str = "";
				for (int i = 0; i < contacts.size(); i++) {
					str = str + contacts.get(i).getName() + key;
				}
				tv.setText(str);
			}

		});
		btn3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				IContactService cs = ClientServiceHelper.getContactService();
				nowContact.setAddress("address");
				nowContact.setDepartment("department");
				nowContact.setEmail("email");
				nowContact.setGroupId(0);
				nowContact.setId(1);
				nowContact.setMobile("2222222222");
				nowContact.setName("name");
				nowContact.setNamePinyin("name");
				nowContact.setNickname("nickname");
				nowContact.setNote("note");
				nowContact.setPhone("11111111111");
				Map<String, Object> result = cs.changeInfo(nowContact);
				String str = (String)result.get("status");
				String msg = (String)result.get("msg");
				tv.setText(str + " " + msg);
			}

		});
		btn4.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				userMgr.addUser(user);
			}

		});
		btn5.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				int id = userMgr.getType(user.getContactId());
				tv.setText(""+id);
			}

		});
	}

}
